package com.example.onlinemonitoring.comCenter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Zxdblqsensor;
import com.example.onlinemonitoring.serviceImpl.ZxdblqsensorServiceImpl;
import org.springframework.context.annotation.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service("zxdblq61850Com")
@Scope("prototype")
public class Zxdblq61850Com extends CommPortManager {
    @Autowired
    ZxdblqsensorServiceImpl blqserviceimpl;

    @Override
    public void receiveData(String str) {
        setText("收到sanway请求：" + str + "\r\n");
        boolean flag = str.length() >= 16 && str.substring(6, 8).equals("A6");
        String s = "";
        if (flag) {
            int deviceaddr = parseHex4(str.substring(8, 12));
            Zxdblqsensor blq = new Zxdblqsensor();
            blq.setStationid(ad.getStationid());
            blq.setDevicegroup(ad.getDevicegroup());
            blq.setDeviceaddr(deviceaddr);
            //blq.setPointaddr(deviceaddr);
            blq = blqserviceimpl.getModel(blq);
            if (blq == null)
                blq = new Zxdblqsensor();
            s += "FF55";// 起始标志位
            s += "65";// 数据长度
            s += "A6";// 功能码
            s += str.substring(8, 12);
            s += String.format("%04x", isnull(blq.getLastthundercount()));// A电击次数
            s += gethexdt(isnull(new Date()));
            s += IEEE754Utils.singleToHex(isnull(blq.getLastwcurrent()));// A基波有效值
            s += IEEE754Utils.singleToHex(isnull(blq.getLastrcurrent()));// A阻性电流
            s += String.format("%02x", getalarmwarnlevel(isnull(blq.getLastwalarmstatus())));// A全电流预警
            s += String.format("%02x", getalarmalarmlevel(isnull(blq.getLastwalarmstatus())));// A全电流报警
            s += String.format("%02x", getalarmwarnlevel(isnull(blq.getLastralarmstatus())));// A阻性电流预警
            s += String.format("%02x", getalarmalarmlevel(isnull(blq.getLastralarmstatus())));// A阻性电流报警

            s += String.format("%02x", isnull(blq.getLastalarmlevel()));// 报警状态
            if (isnull(blq.getLastcommunicationstatus()) == -1)
                blq.setLastcommunicationstatus(1);
            s += String.format("%02x", isnull(blq.getLastcommunicationstatus()));// 通讯状态
            s += CRC16.calcCrc16(s);
            Send(s);
        }
    }
    public static int getalarmalarmlevel(int level){
        switch (level)
        {
            case 2: return 1;
            default: return 0;
        }
    }
    public static int getalarmwarnlevel(int level){
        switch (level)
        {
            case 1: return 1;
            default: return 0;
        }
    }
    public static String gethexdt(Date time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss");
        String[] sdatea = new String[6];
        try {
            sdatea = sdf.format(HexUtil.todate(time)).split(",");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return String.format("%04x", Integer.parseInt(sdatea[0])) + String.format("%02x", Integer.parseInt(sdatea[1]))
        + String.format("%02x", Integer.parseInt(sdatea[2])) + String.format("%02x", Integer.parseInt(sdatea[3]))
        + String.format("%02x", Integer.parseInt(sdatea[4])) + String.format("%02x", Integer.parseInt(sdatea[5]));
    }


    @Async("threadPool")
    public void startReceiveComThread(String str) {
        socketConnectType = 1;//短连接
        isrecv.put(Thread.currentThread().getId(), true);
       // while (isrecv.get(Thread.currentThread().getId()) != null) {
            //recvSum("");
            recv(str);
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        //}
    }
}
